package com.kaifamiao.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.logging.Logger;

public class HelloFilter implements Filter {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    private FilterConfig config ;

    @Override
    public void init(FilterConfig config) throws ServletException {
        logger.info( "初始化" );
        // 容器通过 FilterConfig 对象向 Filter 传递数据
        this.config = config ;
    }

    @Override
    public void doFilter( ServletRequest req , ServletResponse resp , FilterChain chain )
            throws IOException, ServletException {
        logger.info( "执行过滤" );
        HttpServletRequest request = (HttpServletRequest) req ;
        logger.info( String.valueOf( request == req ) );
        String uri = request.getRequestURI();
        logger.info( "正在过滤: " + uri );

        // 通过过滤器链(FilterChain)向后传递 请求 和 响应
        chain.doFilter( req , resp );
    }

    @Override
    public void destroy() {
        logger.info( "销毁" );
    }

}
